import { mongo } from "../mogoConfig"

/**
 * count
 * 根据条件查询数量
 */

const run = async () => {
    const db = mongo.db("sample_mflix")
    const moiveTB = db.collection("movies")

    const result1 = await db.command({
        count: "movies",
        query: { year: { $gte: 1914 } },
        limit: 10
    })
    console.log("结果1-----------------------------")
    console.log(JSON.stringify(result1))

    const result2 = await moiveTB.countDocuments({ year: { $gte: 1914 } })
    console.log("结果2-----------------------------")
    console.log(JSON.stringify(result2))

    const result3 = await moiveTB.aggregate([
        { $match: { year: { $gte: 1914 } } },
        { $count: "总数" }
    ])
    console.log("结果3-----------------------------")
    for await (const item of result3) {
        console.log(JSON.stringify(item))
    }

}



run()
    .catch((error) => {
        console.log(error)
    })
    .finally(() => {
        mongo.close()
        console.log("执行结束")
    })